package com.woniuxy.factory.controller;

import com.woniuxy.factory.dto.CheckLogDTO;
import com.woniuxy.factory.entity.CheckLog;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.service.CheckLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.*;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/checkLog")
public class CheckEquipmentLogController {
    @Autowired
    CheckLogService checkLogService;

    @ResponseBody
    @RequestMapping("/show")
    public ResponseResult showCheckLog(CheckLog checkLog, int page, int limit) {
        ResponseResult<Object> res = new ResponseResult<>();
        List<CheckLogDTO> checkLogs = checkLogService.showCheckLogBCodition(checkLog, page, limit);
        res.setCode(0);
        res.setMsg("查询成功");
        res.setData(checkLogs);
        return res;
    }

    @ResponseBody
    @RequestMapping("/add")
    //每日设备管理人员需对自己负责车间设备进行检查，并填写备注。
    public ResponseResult addCheckLog(CheckLog checkLog) {
        ResponseResult<Object> res = new ResponseResult<>();
        //参数验证
        String remarks = checkLog.getRemarks();
        String empname = checkLog.getEmpname();
        String wosId = checkLog.getWosId();
        if (remarks == null || empname == null || wosId == null) {
            res.setCode(1);
            res.setMsg("非法参数");
            return res;
        }
        if (remarks.isEmpty() || empname.isEmpty() || wosId.isEmpty()) {
            res.setCode(1);
            res.setMsg("非法参数");
            return res;
        }

        try {
            checkLogService.addCheckLog(checkLog);
        } catch (Exception e) {
            res.setCode(0);
            res.setMsg(e.getMessage());
            return res;
        }

        res.setCode(0);
        res.setMsg("添加成功");
        return res;
    }

    @ResponseBody
    @RequestMapping("/modify")
    //对于填写错误的日志可以修改，但是日期不行
    public ResponseResult modifyCheckLog(CheckLog checkLog) {
        ResponseResult<Object> res = new ResponseResult<>();
        //参数验证
        String remarks = checkLog.getRemarks();
        String empname = checkLog.getEmpname();
        String wosId = checkLog.getWosId();
        if (remarks == null || empname == null || wosId == null) {
            res.setCode(1);
            res.setMsg("非法参数");
            return res;
        }
        if (remarks.isEmpty() || empname.isEmpty() || wosId.isEmpty()) {
            res.setCode(1);
            res.setMsg("非法参数");
            return res;
        }
        checkLogService.modifyCheckLog(checkLog);
        res.setCode(0);
        res.setMsg("修改成功");
        return res;
    }
}
